/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.brain.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.brain.entity.BUserOrprInfo;
import com.thinkgem.jeesite.modules.brain.dao.BUserOrprInfoDao;
import com.thinkgem.jeesite.modules.brain.entity.BOperRecordDetial;
import com.thinkgem.jeesite.modules.brain.dao.BOperRecordDetialDao;

/**
 * 用户答题操作记录Service
 * @author 程展峰
 * @version 2021-09-22
 */
@Service
@Transactional(readOnly = true)
public class BUserOrprInfoService extends CrudService<BUserOrprInfoDao, BUserOrprInfo> {

	@Autowired
	private BOperRecordDetialDao bOperRecordDetialDao;
	
	public BUserOrprInfo get(String id) {
		BUserOrprInfo bUserOrprInfo = super.get(id);
		bUserOrprInfo.setBOperRecordDetialList(bOperRecordDetialDao.findList(new BOperRecordDetial(bUserOrprInfo)));
		return bUserOrprInfo;
	}
	
	public List<BUserOrprInfo> findList(BUserOrprInfo bUserOrprInfo) {
		return super.findList(bUserOrprInfo);
	}
	
	public Page<BUserOrprInfo> findPage(Page<BUserOrprInfo> page, BUserOrprInfo bUserOrprInfo) {
		return super.findPage(page, bUserOrprInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(BUserOrprInfo bUserOrprInfo) {
		super.save(bUserOrprInfo);
		for (BOperRecordDetial bOperRecordDetial : bUserOrprInfo.getBOperRecordDetialList()){
			if (bOperRecordDetial.getId() == null){
				continue;
			}
			if (BOperRecordDetial.DEL_FLAG_NORMAL.equals(bOperRecordDetial.getDelFlag())){
				if (StringUtils.isBlank(bOperRecordDetial.getId())){
					bOperRecordDetial.setOprId(bUserOrprInfo);
					bOperRecordDetial.preInsert();
					bOperRecordDetialDao.insert(bOperRecordDetial);
				}else{
					bOperRecordDetial.preUpdate();
					bOperRecordDetialDao.update(bOperRecordDetial);
				}
			}else{
				bOperRecordDetialDao.delete(bOperRecordDetial);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BUserOrprInfo bUserOrprInfo) {
		super.delete(bUserOrprInfo);
		bOperRecordDetialDao.delete(new BOperRecordDetial(bUserOrprInfo));
	}
	
}